package com.example.controller;

import com.example.entity.WorkItem;
import com.example.service.WorkItemService;
import com.example.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/workitems")
public class WorkItemController {

    @Autowired
    private WorkItemService workItemService;

    /**
     * 获取所有工作项
     */
    @GetMapping
    public Result getAllWorkItems() {
        try {
            List<WorkItem> workItems = workItemService.getAllWorkItems();
            return Result.success(workItems);
        } catch (Exception e) {
            return Result.error("获取工作项列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据项目ID获取工作项
     */
    @GetMapping("/project/{projectId}")
    public Result getWorkItemsByProjectId(@PathVariable Integer projectId) {
        try {
            if (projectId == null || projectId <= 0) {
                return Result.error("项目ID不能为空或无效");
            }
            List<WorkItem> workItems = workItemService.getWorkItemsByProjectId(projectId);
            return Result.success(workItems);
        } catch (Exception e) {
            return Result.error("获取项目工作项失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取工作项详情
     */
    @GetMapping("/{id}")
    public Result getWorkItemById(@PathVariable Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.error("工作项ID不能为空或无效");
            }
            WorkItem workItem = workItemService.getWorkItemById(id);
            if (workItem != null) {
                return Result.success(workItem);
            } else {
                return Result.error("工作项不存在");
            }
        } catch (Exception e) {
            return Result.error("获取工作项详情失败: " + e.getMessage());
        }
    }

    /**
     * 创建工作项
     */
    @PostMapping
    public Result createWorkItem(@RequestBody WorkItem workItem) {
        try {
            if (workItem == null) {
                return Result.error("工作项信息不能为空");
            }
            if (workItem.getTitle() == null || workItem.getTitle().trim().isEmpty()) {
                return Result.error("工作项标题不能为空");
            }
            workItemService.createWorkItem(workItem);
            return Result.success("工作项创建成功");
        } catch (Exception e) {
            return Result.error("工作项创建失败: " + e.getMessage());
        }
    }

    /**
     * 更新工作项
     */
    @PutMapping("/{id}")
    public Result updateWorkItem(@PathVariable Integer id, @RequestBody WorkItem workItem) {
        try {
            if (id == null || id <= 0) {
                return Result.error("工作项ID不能为空或无效");
            }
            if (workItem == null) {
                return Result.error("工作项信息不能为空");
            }
            workItem.setWorkItemId(id);
            workItemService.updateWorkItem(workItem);
            return Result.success("工作项更新成功");
        } catch (Exception e) {
            return Result.error("工作项更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除工作项
     */
    @DeleteMapping("/{id}")
    public Result deleteWorkItem(@PathVariable Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.error("工作项ID不能为空或无效");
            }
            workItemService.deleteWorkItem(id);
            return Result.success("工作项删除成功");
        } catch (Exception e) {
            return Result.error("工作项删除失败: " + e.getMessage());
        }
    }

    /**
     * 根据状态获取工作项
     */
    @GetMapping("/status/{status}")
    public Result getWorkItemsByStatus(@PathVariable String status) {
        try {
            if (status == null || status.trim().isEmpty()) {
                return Result.error("状态不能为空");
            }
            List<WorkItem> workItems = workItemService.getWorkItemsByStatus(status);
            return Result.success(workItems);
        } catch (Exception e) {
            return Result.error("根据状态获取工作项失败: " + e.getMessage());
        }
    }

    /**
     * 根据负责人获取工作项
     */
    @GetMapping("/assignee/{responsiblePerson}")
    public Result getWorkItemsByResponsiblePerson(@PathVariable String responsiblePerson) {
        try {
            if (responsiblePerson == null || responsiblePerson.trim().isEmpty()) {
                return Result.error("负责人不能为空");
            }
            List<WorkItem> workItems = workItemService.getWorkItemsByResponsiblePerson(responsiblePerson);
            return Result.success(workItems);
        } catch (Exception e) {
            return Result.error("根据负责人获取工作项失败: " + e.getMessage());
        }
    }

    /**
     * 根据负责人统计工作项数量，可按项目ID筛选
     */
    @GetMapping("/assignee/{username}/count")
    public Result countWorkItemsByAssignee(
            @PathVariable String username,
            @RequestParam(required = false) Integer projectId) {
        try {
            if (username == null || username.trim().isEmpty()) {
                return Result.error("负责人用户名不能为空");
            }
            int count = workItemService.countWorkItemsByResponsiblePerson(username, projectId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("统计负责人任务数量失败: " + e.getMessage());
        }
    }
}